<!--
 * @Description: 单位管理页面
-->
<template>
  <BaseMainSlot :show-tab="true">
    <template v-slot:btn-box>
      <ActionBar :page-no="65" :add-active="addActive" :del-active="delActive" :has-edit="false" :has-save="false" :has-cancel="false" @add="add" @del="del" />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
    </template>
    <template v-slot:tab-select>
      <SelectButton :buttonselectlist="buttonselectlist" @nowselectbuttonindex="batchstaumet" />
    </template>
    <template v-slot:table-box>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="92%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="inventoryTask.inventoryTaskName" label="任务名称" />
        <el-table-column prop="warehouseName" label="盘点仓库" />
        <el-table-column label="查看产品">
          <template v-if="scope.row.inventoryRecordNo" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small" @click="checkProduct(scope.row)"><i class="el-icon-view"></i> 查看产品</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="inventoryTask.personName" label="盘点人" />
        <el-table-column prop="executionDate" label="执行日期" />
        <el-table-column label="盘点结果">
          <template v-if="scope.row.inventoryRecordNo" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small" @click="checkResult(scope.row)"><i class="el-icon-view"></i> 查看产品</el-button>
          </template>
        </el-table-column>
        <el-table-column v-if="curTabIndex == 0" width="140" label="操作">
          <template v-if="scope.row.inventoryRecordNo" slot-scope="scope">
            <el-button v-if="judgePermission(65, 'taskVerify')" class="text-btn-cursor" type="text" size="small" @click="handleDelete(scope.row)"><i class="el-icon-check"></i> 通过</el-button>
            <el-button v-if="judgePermission(65, 'taskVerify')" class="text-btn-cursor cursor-delete" type="text" size="small" @click="handleEdit(scope.row)"><i class="el-icon-close"></i> 不通过</el-button>
          </template>
        </el-table-column>
        <el-table-column v-if="curTabIndex == 2" width="140" label="操作">
          <template v-if="scope.row.inventoryRecordNo" slot-scope="scope">
            <el-popover
              placement="top-start"
              title="拒绝原因"
              width="200"
              trigger="hover"
              :content="scope.row.reason"
            >
              <el-button slot="reference" class="text-btn-cursor" type="text" size="small">查看原因</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="'拒绝原因'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          class="el-form"
        >
          <el-form-item label="拒绝理由:" prop="reason" class="w100p">
            <el-input v-model="formData.reason" clearable />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">
            保 存
          </el-button>
        </div>
      </el-dialog>

      <el-dialog :title="'产品详情'" :visible.sync="dialogProductFormVisible" width="40%">
        <div class="product-detail">
          <div class="title">产品列表：</div>
          <div class="product-list">
            <div v-for="(item, index) in curProductList" :key="index" class="product-item">
              {{ item.goodsName }}
            </div>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="closeProduct()">
            关 闭
          </el-button>
        </div>
      </el-dialog>
      <el-dialog :title="'盘点详情'" :visible.sync="dialogResultFormVisible" width="40%">
        <el-form
          ref="formData"
          label-position="left"
          :model="resultFormData"
          class="el-form"
        >
          <el-form-item label="处理日期:" prop="executionDate" class="w100p">
            <el-date-picker v-model="resultFormData.executionDate" disabled align="right" type="date" placeholder="选择日期" />
          </el-form-item>
          <el-form-item label="盘点详情:" prop="curProductList" class="w100p">
            <div class="productList">
              <div v-for="(item, index) in curProductList" :key="index" class="product-item">
                <div class="product-name">{{ item.goodsName }}</div>
                <div class="product-value">仓库剩余量: {{ item.value }} {{ item.unitName }}</div>
                <div class="product-inventoryValue">
                  <el-form-item label="盘点后剩余量:" prop="item.inventoryValue" class="w100p">
                    <el-input v-model="item.inventoryValue" disabled />
                  </el-form-item>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="closeResult()">
            关 闭
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from '@/components/searchForm';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/digital-operation/warehouse-check";
import warehouseManage from "@/api/basic-info-manage/warehouse-manage";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot"
import SelectButton from "@/components/selectbutton/index.vue"
import staffManage from "@/api/digital-operation/personnel-cost-mansge/staff-manage/index";

import { mymixin } from "@/utils/mixin"
import { mapGetters } from 'vuex';
export default {
  // eslint-disable-next-line vue/name-property-casing
  name: "check-result-examine",
  components: { SearchForm, ActionBar, Pagination, BaseMainSlot, SelectButton },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: '任务名称',
          prop: 'inventoryTaskName',
          type: 'input',
        }, {
          label: '盘点仓库',
          prop: 'warehouseNo',
          type: 'select',
          options: [], // warehouseList
          optionKey: { label: 'warehouseName', value: 'warehouseNo' },
        }, {
          label: '盘点人',
          prop: 'personNo',
          type: 'select',
          options: [], // personList
          optionKey: { label: 'personName', value: 'personNo' },
        }, {
          label: '执行日期',
          prop: 'executionDate',
          type: 'date',
        }, 
      ],
      searchlist: {},
      unitList: [],
      dialogTitle: "",
      dialogFormVisible: false,
      buttonselectlist: [
        { name: "未审核" },
        { name: "已审核" }
      ],
      curTabIndex: 0,
      warehouseList: [],
      productList: [],
      personList: [],
      dialogProductFormVisible: false,
      dialogResultFormVisible: false,
      curProductList: [],
      resultFormData: {}
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    // 获取表格数据
    this.getTableDataList();
    this.getWarehouseList();
    this.getPersonList();
  },
  mounted() { },
  methods: {
    checkProduct(row) {
      if (row.inventoryTaskNo) {
        this.dialogProductFormVisible = true;
        nowpagereq.queryAllByInventoryTaskNo(row.inventoryTaskNo).then(result => {
          this.curProductList = result.data;
        });
      }
    },
    closeProduct() {
      this.curProductList = [];
      this.dialogProductFormVisible = false;
    },
    checkResult(row) {
      if (row.inventoryTaskNo) {
        this.resultFormData = row;
        this.dialogResultFormVisible = true;
        nowpagereq.getInventoryRecord(row.inventoryRecordNo).then(result => {
          this.curProductList = result.data;
        });
      }
    },
    closeResult() {
      this.curProductList = [];
      this.dialogResultFormVisible = false;
    },
    batchstaumet(index) {
      this.curTabIndex = index;
      this.getTableDataList();
    },
    getWarehouseList() {
      let params = { entity: {} };
      warehouseManage.getWareHouseListByCondition(params).then(res => {
        this.warehouseList = res.data;
        this.setOptions(this.formConfig, 'warehouseNo', res.data)
      })
    },
    getPersonList() {
      // let params = { entity: {} };
      staffManage.getStaffListByCondition().then(res => {
        this.personList = res.data;
        let list = res.data.map(el => {
          return { ...el, personName: el.person.name }
        })
        this.setOptions(this.formConfig, 'personNo', list)
      })
    },
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          auditStatus: this.curTabIndex,
          ...this.searchlist
        }
      };
      nowpagereq.queryRecordPageByCondition(params).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error('查询失败')
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet()
        this.resetActionBar();
      });
      // }
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    async handleEdit(row) {
      if (row.inventoryRecordNo) {
        this.openDialog('edit')
        this.formData = JSON.parse(JSON.stringify(row));
      }
    },
    // 添加
    add() {
      this.openDialog('add')
      this.formData = {}
    },
    // 取消
    cancel() {
      this.closeDialog();
      this.formData = {}
    },
    // 初始化操作栏状态
    resetActionBar() {
      this.nowChanceStau = ''
      this.formData = {}
    },
    handleSelectionChange(list) {
      this.selectedPlantingUnitList = [];
      this.selectedPlantingUnitList = list.map(item => {
        if (item.unitNo) {
          return item.unitNo
        } else {
          return ""
        }
      });
    },
    delmessage() {
      nowpagereq.delPlantingUnitById(this.selectedPlantingUnitList).then(
        res => {
          if (res.code === 1) {
            this.$message.success('删除成功')
            this.getTableDataList();
          } else {
            this.$message.error('删除失败')
          }
        }
      )
    },
    handleDelete(data) {
      this.$alert('确认通过该记录', '通过盘点记录', {
        confirmButtonText: '确定',
        callback: action => {
          if (action === 'confirm') {
            let params = {
              inventoryRecordNo: data.inventoryRecordNo,
              auditStatus: 1,
              auditTime: new Date()
            };
            nowpagereq.editRecord(params).then(res => {
              if (res.code !== 1) {
                return;
              }
              this.$message.success('操作成功')
              this.getTableDataList();
            })
          }
        }
      });
    },
    save() {
      let params = {
        auditStatus: 2,
        reason: this.formData.reason,
        inventoryTaskNo: this.formData.inventoryTaskNo,
        auditTime: new Date()
      };
      // 编辑接口
      nowpagereq.editRecord(params).then(res => {
        if (res.code === 1) {
          this.$message.success("操作成功！");
          this.closeDialog();
          this.getTableDataList();
        } else {
          this.$message.error("操作失败！");
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === 'add' ? '新增' : '编辑'
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = '';
    }
  }
};
</script>

<style scoped lang="scss">
.df {
  display: flex;
  align-items: center;
  width: 100%;
}

.dfs {
  ::v-deep .el-form-item__content {
    flex-direction: column;
  }
  i {
    font-size: 30px;
    margin-left: 10px;
    cursor: pointer;
  }
}

.tags {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .tag-item {
    height: 30px;
    padding: 5px 10px;
    background-color: #00a1c2;
    border-radius: 8px;
    display: flex;
    align-items: center;
    margin-top: 10px;
    margin-right: 10px;

    i {
      font-size: 14px;
    }
  }
}
.productList{

  .product-item{
    display: flex;
    margin-bottom: 10px;
    .product-name{
      width: 120px;
      flex-shrink: 0;
      font-size: 16px;
    }
    .product-value{
      font-size: 16px;
      width: 200px;
      flex-shrink: 0;
    }
    .product-inventoryValue{
      width: 100%;
    }
  }
}
.product-detail{
  .title{
    font-size: 16px;
    font-weight: bold;
  }
  .product-list{
    display: flex;
    flex-wrap: wrap;

    .product-item{
      background: #00a1c2;
      color: #fff;
      text-align: center;
      padding: 8px 24px;
      margin: 10px 20px 0 0;
      border-radius: 8px;
    }
  }
}
</style>
